package com.aisino.api.config;

import com.aisino.api.model.Entity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class EntityCacheConfig implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(EntityCacheConfig.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 缓存企业信息，key为id，value为jkmy
    public static final Map<String, Entity> ENTITY_CACHE = new HashMap<>();

    public static final Map<String, String> YZ_CACHE = new HashMap<>();

    @Override
    public void run(String... args) throws Exception {
        // 在Spring Boot启动后执行，初始化所有已开通接口的企业信息到内存
        String sql = "select id,jkmy,dqbm,tyxydm,qymc from YGYD_MAIN.ENTITY where SFKTJK=1 and DELETE_FLAG='00'";
        List<Entity> result = jdbcTemplate.query(sql, (rs, rowNum) -> {
            Entity entity = new Entity();
            entity.setId(rs.getString("id"));
            entity.setJkmy(rs.getString("jkmy"));
            entity.setDqbm(rs.getString("dqbm"));
            entity.setTyxydm(rs.getString("tyxydm"));
            entity.setQymc(rs.getString("qymc"));
            return entity;
        });

        // 将查询结果缓存到内存中
        for (Entity row : result) {
            String id = row.getId();
            ENTITY_CACHE.put(id, row);
        }

        logger.info("成功初始化企业信息到内存，共加载 {} 条记录", ENTITY_CACHE.size());

        sql="select CONFIG_KEY,CONFIG_VALUE from YGYD_SYS.DEV_CONFIG where CATEGORY='BIZ_DEFINE' and DELETE_FLAG='00'";
        List<Map<String, Object>> resultYz = jdbcTemplate.queryForList(sql);
        for (Map<String, Object> row : resultYz) {
            String CONFIG_KEY = (String) row.get("CONFIG_KEY");
            String CONFIG_VALUE = (String) row.get("CONFIG_VALUE");
            YZ_CACHE.put(CONFIG_KEY, CONFIG_VALUE);
        }
        logger.info("成功初始化业务定义信息到内存，共加载 {} 条记录", YZ_CACHE.size());

    }

    public static String getJkmy(String id,JdbcTemplate jdbcTemplate) {
        Entity entity = ENTITY_CACHE.get(id);
        if (entity == null) {
            reInitChahe(id,jdbcTemplate);
            entity = ENTITY_CACHE.get(id);
        }
        return entity!=null?entity.getJkmy():null;
    }

    private static void reInitChahe(String id,JdbcTemplate jdbcTemplate){
        String sql = "select id,jkmy,dqbm,tyxydm,qymc from YGYD_MAIN.ENTITY where SFKTJK=1 and DELETE_FLAG='00' and id=?";
        List<Entity> result = jdbcTemplate.query(sql, (rs, rowNum) -> {
            Entity entity = new Entity();
            entity.setId(rs.getString("id"));
            entity.setJkmy(rs.getString("jkmy"));
            entity.setDqbm(rs.getString("dqbm"));
            entity.setTyxydm(rs.getString("tyxydm"));
            entity.setQymc(rs.getString("qymc"));
            return entity;
        }, id);
        if (result.isEmpty()) {
        }else {
            ENTITY_CACHE.put(id, result.get(0));
        }
    }
}